package cn.stephen.study.demoproject.controller;


import cn.stephen.study.demoproject.dao.dbtwo.StationEquipmentMapper;
import cn.stephen.study.demoproject.entity.SBJCYJ;
import cn.stephen.study.demoproject.entity.StationEquipment;
import cn.stephen.study.demoproject.entity.TestEntity;
import cn.stephen.study.demoproject.service.SBJCYJService;
import cn.stephen.study.demoproject.service.TestService;
import cn.stephen.study.demoproject.util.WordUtil;
import net.sf.json.JSONArray;
import net.sf.json.JSONObject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletResponse;
import java.util.List;

@Controller
public class TestController {

    @Autowired
    private TestService testService;
    @Autowired
    private SBJCYJService sbjcyjService;
    @Autowired
    private StationEquipmentMapper stationEquipmentMapper;

    @RequestMapping(value = "/get/{id}", method = RequestMethod.GET)
    public TestEntity test(@PathVariable Integer id) {
        System.out.println("id:" + id);
        return testService.getById(id);
    }

    @RequestMapping("/")
    public String hello() {
        return "forward:login.html";
    }

    @RequestMapping(value = "show", method = RequestMethod.GET)
    public String show(Model model) {
        model.addAttribute("uid", "123456789");
        model.addAttribute("name", "Jerry");
        return "show";
    }

    @RequestMapping(value = "getAll", method = RequestMethod.GET)
    @ResponseBody
    public JSONObject getAll() {
        JSONObject jb = new JSONObject();
        List<SBJCYJ> sbjcyj = sbjcyjService.findAll();
        JSONArray jsonArray = new JSONArray();
        for (SBJCYJ sbjcyj1 : sbjcyj) {
            JSONObject jsonObject = new JSONObject();
            jsonObject.put("SBJCYJ010", sbjcyj1.getSBJCYJ010());
            jsonArray.add(jsonObject);
        }
        jb.put("data", jsonArray);
        return jb;
    }

    @RequestMapping("findByValues")
    @ResponseBody
    public JSONObject findByValues() {
        JSONObject jo = new JSONObject();
        try {
            jo.put("state", 0);
            jo.put("msg", "查询成功");
        } catch (Exception e) {
            e.printStackTrace();
            jo.put("state", 1);
            jo.put("msg", "查询失败");
        }
        return jo;
    }

    @RequestMapping(value = "/download")
    @ResponseBody
    public void downloadInfo(HttpServletResponse response, Integer id) {
        //if (doSecurityIntercept(Const.RESOURCES_TYPE_BUTTON){
        try {
            WordUtil wd = new WordUtil();
            wd.download(response, "42098100200604000000", 2);
//            wd.exportListWord();

        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    @RequestMapping(value = "/find")
    @ResponseBody
    public List<StationEquipment> find() {
//            sbjcyjService.getValue(2, "03111800014", 0);
            List<StationEquipment> all = stationEquipmentMapper.findAll("42098110800204000000", null, null);
            System.out.println(all);
        return all;
    }
}
